package cn.com.hbhgkj.webapp.taglib;

import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;

import javax.servlet.jsp.JspException;

import org.apache.struts2.views.jsp.ui.SelectTag;

import cn.com.hbhgkj.model.LabelValue;

import com.opensymphony.xwork2.ActionContext;

public class ProvinceTag extends SelectTag {

	public int doStartTag() throws JspException {
		List<LabelValue> targetList = buildProvinceList(pageContext.getRequest().getLocale());
		ActionContext.getContext().getValueStack()
				.set("crs_provinces", targetList);
		this.setList("%{crs_provinces}");
		return super.doStartTag();
	}

	/**
	 * Build a List of LabelValues for all the available countries. Uses the two
	 * letter uppercase ISO name of the country as the value and the localized
	 * country name as the label.
	 * 
	 * @param locale
	 *            The Locale used to localize the country names.
	 * 
	 * @return List of LabelValues for all available countries.
	 */
	@SuppressWarnings("unchecked")
	private List<LabelValue> buildProvinceList(Locale locale) {
		List<LabelValue> provinces = new ArrayList<LabelValue>();
		provinces.add(new LabelValue("湖北","湖北"));
		provinces.add(new LabelValue("境外","境外"));
		provinces.add(new LabelValue("北京","北京"));
		provinces.add(new LabelValue("上海","上海"));
		provinces.add(new LabelValue("天津","天津"));
		provinces.add(new LabelValue("内蒙古","内蒙古"));
		provinces.add(new LabelValue("山西","山西"));
		provinces.add(new LabelValue("河北","河北"));
		provinces.add(new LabelValue("辽宁","辽宁"));
		provinces.add(new LabelValue("吉林","吉林"));
		provinces.add(new LabelValue("黑龙江","黑龙江"));
		provinces.add(new LabelValue("江苏","江苏"));
		provinces.add(new LabelValue("安徽","安徽"));
		provinces.add(new LabelValue("山东","山东"));
		provinces.add(new LabelValue("浙江","浙江"));
		provinces.add(new LabelValue("江西","江西"));
		provinces.add(new LabelValue("福建","福建"));
		provinces.add(new LabelValue("湖南","湖南"));
		provinces.add(new LabelValue("河南","河南"));
		provinces.add(new LabelValue("广东","广东"));
		provinces.add(new LabelValue("海南","海南"));
		provinces.add(new LabelValue("广西","广西"));
		provinces.add(new LabelValue("贵州","贵州"));
		provinces.add(new LabelValue("四川","四川"));
		provinces.add(new LabelValue("云南","云南"));
		provinces.add(new LabelValue("陕西","陕西"));
		provinces.add(new LabelValue("甘肃","甘肃"));
		provinces.add(new LabelValue("宁夏","宁夏"));
		provinces.add(new LabelValue("青海","青海"));
		provinces.add(new LabelValue("新疆","新疆"));
		provinces.add(new LabelValue("西藏","西藏"));
		provinces.add(new LabelValue("重庆","重庆"));
		provinces.add(new LabelValue("香港","香港"));
		provinces.add(new LabelValue("澳门","澳门"));
		provinces.add(new LabelValue("台湾","台湾"));
		
		//Collections.sort(provinces, new LabelValueComparator(locale));

		return provinces;
	}

	/**
	 * Class to compare LabelValues using their labels with locale-sensitive
	 * behaviour.
	 */
	public class LabelValueComparator implements Comparator {
		private Comparator c;

		/**
		 * Creates a new LabelValueComparator object.
		 * 
		 * @param locale
		 *            The Locale used for localized String comparison.
		 */
		public LabelValueComparator(final Locale locale) {
			c = Collator.getInstance(locale);
		}

		/**
		 * Compares the localized labels of two LabelValues.
		 * 
		 * @param o1
		 *            The first LabelValue to compare.
		 * @param o2
		 *            The second LabelValue to compare.
		 * 
		 * @return The value returned by comparing the localized labels.
		 */
		@SuppressWarnings("unchecked")
		public final int compare(Object o1, Object o2) {
			LabelValue lhs = (LabelValue) o1;
			LabelValue rhs = (LabelValue) o2;

			return c.compare(lhs.getLabel(), rhs.getLabel());
		}
	}
}
